﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Collections.Specialized;
using System.Data;
using HY.ConfigurationSectionHandler;
using System.Drawing;
using PublicCommon;

namespace HYDataGridView
{
    public class Helper
    {
        private Helper()
        {
        }

        public static void SetUsefulLifeOverWarning(String TableName,ref DataGridView dgv)
        {
            foreach (DataGridViewRow dgvr in dgv.Rows)
            {
                //警告
                if (Functions.HasCardWarning(Convert.ToDateTime(dgvr.Cells[String.Concat(TableName,"_StartDate")].Value), Convert.ToDateTime(dgvr.Cells[String.Concat(TableName,"_EndDate")].Value)))
                {
                    dgvr.DefaultCellStyle.ForeColor = System.Drawing.Color.FromArgb(-2790912);
                    continue;
                }
                //失效
                if (Functions.HasCardVaild(Convert.ToDateTime(dgvr.Cells[String.Concat(TableName,"_StartDate")].Value), Convert.ToDateTime(dgvr.Cells[String.Concat(TableName,"_EndDate")].Value)))
                {
                    dgvr.DefaultCellStyle.ForeColor = Color.Red;
                    continue;
                }
                
                if(dgvr.DefaultCellStyle.ForeColor == Color.Red)
                {
                	dgvr.DefaultCellStyle.ForeColor = Color.Black;
                }
            }
        }

        public static void InitDataGridViewColumn(String strConfigName,ref DataGridView dgv)
        {
            List<DataGridViewConfig> lDataGridViewConfig = ReadConfig.GetConfig<DataGridViewConfig>(strConfigName);
            DataGridViewColumn dgvc;
            foreach (DataGridViewConfig dgvcConfig in lDataGridViewConfig)
            {
                dgvc = CreateColumn(dgvcConfig);
                dgvc.Name = dgvcConfig.Name;
                dgvc.HeaderText = dgvcConfig.HeaderText;
                dgvc.Width = Int32.Parse(dgvcConfig.Width);
                dgvc.DataPropertyName = dgvcConfig.Name;
                dgvc.ReadOnly = true;
                dgvc.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
                dgv.Columns.Add(dgvc);
            }
        }

        public static DataRow GetDataGridViewSelectedRow(Form fm,DataGridView dgv)
        {
            //取得当前行
            if (dgv.DataSource == null)
            {
                MessageBox.Show("请选定需要的行");
                return null;
            }
            CurrencyManager MyCurrencyManager = fm.BindingContext[dgv.DataSource, dgv.DataMember] as CurrencyManager;            
            if (0 == MyCurrencyManager.Count)
            {
                MessageBox.Show("请选定需要的行");
                return null;
            }
            return (MyCurrencyManager.Current as DataRowView).Row;
        }

        public static int? GetDataGridViewSelectedPosition(Form fm,DataGridView dgv)
        {
            //取得当前行
            if (dgv.DataSource == null)
            {                
                return null;
            }
            CurrencyManager MyCurrencyManager = fm.BindingContext[dgv.DataSource, dgv.DataMember] as CurrencyManager;
            if (0 == MyCurrencyManager.Count)
            {
                return null;
            }
            return MyCurrencyManager.Position;
        }

        public static void SetDataGridViewSelectedPosition(Form fm, DataGridView dgv, int? Position)
        {
            if (dgv.DataSource == null || !Position.HasValue)
            {
                return;
            }
            CurrencyManager MyCurrencyManager = fm.BindingContext[dgv.DataSource, dgv.DataMember] as CurrencyManager;
            if (0 == MyCurrencyManager.Count)
            {
                return;
            }

            MyCurrencyManager.Position = Position.Value;
        }

        private static DataGridViewColumn CreateColumn(DataGridViewConfig dgvcConfig)
        {
            if ("TEXTBOX" == dgvcConfig.DataGridViewColumnType.ToUpper())
            {
                return new DataGridViewTextBoxColumn();
            }

            if("IMAGE" == dgvcConfig.DataGridViewColumnType.ToUpper())
            {
                DataGridViewImageColumn dgvic = new DataGridViewImageColumn(false);
                dgvic.ImageLayout = DataGridViewImageCellLayout.Zoom;
                return dgvic;
            }
            return null;            
        }
    }
}
